/**
 * 
 */
package com.rrd.dw.mr.key;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.WritableComparable;
import org.apache.hadoop.io.WritableComparator;
import org.apache.hadoop.mapreduce.Partitioner;

/**
 * @author baoxin.liu
 * @version 创建时间：2017年8月28日 下午7:26:50
 *          类说明
 */
public class BHZXPairKey implements WritableComparable<BHZXPairKey> {
    private String first;
    private int second;

    /**
     * @param second
     *        the second to set
     */
    public void setKey(String left, int right) {
        this.first = left;
        this.second = right;
    }

    /**
     * @return the first
     */
    public String getFirst() {
        return first;
    }

    /**
     * @return the second
     */
    public int getSecond() {
        return second;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        // TODO Auto-generated method stub
        out.writeUTF(first);
        out.writeInt(second);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        first = in.readUTF();
        second = in.readInt();
    }

    @Override
    public int compareTo(BHZXPairKey o) {
        if (!first.equals(o.first)) {
            return first.compareTo(o.first);
        } else if (second != o.second) {
            return second < o.second ? -1 : 1;
        } else {
            return 0;
        }
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {
        // TODO Auto-generated method stub
        return (int) (first.hashCode() * 127 + this.second);
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object right) {
        // TODO Auto-generated method stub
        if (right == null)
            return false;
        if (this == right)
            return true;
        if (right instanceof BHZXPairKey) {
            BHZXPairKey r = (BHZXPairKey) right;
            return r.first.equals(this.first) && r.second == this.second;
        } else {
            return false;
        }
    }

    public static class FirstPartitioner
            extends Partitioner<BHZXPairKey, Text> {

        /*
         * (non-Javadoc)
         * @see org.apache.hadoop.mapreduce.Partitioner#getPartition(java.lang.
         * Object, java.lang.Object, int)
         */
        @Override
        public int getPartition(BHZXPairKey key, Text value,
                int numPartitions) {
            // TODO Auto-generated method stub
            return Math.abs(key.getFirst().hashCode() * 127) % numPartitions;
        }

    }

    public static class MyGroupingComparator extends WritableComparator {

        public MyGroupingComparator() {
            super(BHZXPairKey.class, true);
        }

        @Override
        public int compare(Object a, Object b) {
            // TODO Auto-generated method stub
            BHZXPairKey ip1 = (BHZXPairKey) a;
            BHZXPairKey ip2 = (BHZXPairKey) b;
            String l = ip1.getFirst();
            String r = ip2.getFirst();
            return l.equals(r) ? 0 : l.compareTo(r);
        }

    }
}
